﻿using Point2d.Geometry;
using Point2d.Graphics;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Point2d.App
{
    public class Lane : LinesEntity
    {
        public Point2 _from = new Point2();
        public Point2 _to = new Point2();
        public float _width = 1.0f;

        public Lane(GraphicsManager manager, Color color)
            : base(manager, color)
        {
        }

        public void Create()
        {
            float dirX = 0.0f;
            float dirY = 0.0f;
            GetDirection(ref dirX, ref dirY);

            float length = GetLength();

            float relativeDirX = 0.0f;
            float relativeDirY = 0.0f;
            MathUtil.RotateDir(dirX, dirY, 90, ref relativeDirX, ref relativeDirY);

            AddPoint(_from._x + relativeDirX * _width / 2.0f, _from._y + relativeDirY * _width / 2.0f);
            AddPoint(_to._x + relativeDirX * _width / 2.0f, _to._y + relativeDirY * _width / 2.0f);

            int count = 100;
            float step = length * (1.0f / count);
            for (int i = 0; i < count; i += 2)
            {
                AddPoint(_from._x + dirX * step * i, _from._y + dirY * step * i);
                AddPoint(_from._x + dirX * step * (i + 1), _from._y + dirY * step * (i + 1));
            }

            AddPoint(_from._x - relativeDirX * _width / 2.0f, _from._y - relativeDirY * _width / 2.0f);
            AddPoint(_to._x - relativeDirX * _width / 2.0f, _to._y - relativeDirY * _width / 2.0f);
        }

        public void GetDirection(ref float moveDirX, ref float moveDirY)
        {
            moveDirX = _to._x - _from._x;
            moveDirY = _to._y - _from._y;

            float length = GetLength();

            moveDirX = moveDirX / length;
            moveDirY = moveDirY / length;
        }

        public float GetLength()
        {
            float moveDirX = _to._x - _from._x;
            float moveDirY = _to._y - _from._y;
            return (float)(Math.Sqrt(moveDirX * moveDirX + moveDirY * moveDirY));
        }
    }
}
